package cn.kgc.smbms.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import cn.kgc.smbms.projo.User;
import cn.kgc.smbms.service.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

@Controller
public class UserController {
    private Logger logger = Logger.getLogger(UserController.class);
    @Resource
    private UserService userService;
    @RequestMapping(value = "/dologin" ,method = RequestMethod.POST)
    public String login(@RequestParam String userCode, @RequestParam String userPassword,
                        HttpSession session, HttpServletRequest request){
        logger.debug("dologin***********");
        User user=userService.findUserByLogin(userCode,userPassword);
        logger.debug(user);
        if (user!=null){
           // new ArrayList<>().get(1);
            session.setAttribute("userCode",user.getUserName());
            return "redirect:/main.html";
        }else {
            //int i=2/0;
            return "login";
        }

    }
    @RequestMapping("/main.html")
    public String main(HttpSession session){
        if (session.getAttribute("userCode")==null){
            return "redirect:login";
        }
        return "frame";
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("userCode");
        return "login";
    }
}
